package lesson.grade;

import lesson.base.BaseServlet;
import lesson.permission.Role;
import lesson.user.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;
import java.util.List;

@WebServlet("/grade/*")
@Role(User.ROLE_ADMIN)
public class GradeServlet extends BaseServlet {
    private GradeService gradeService = new GradeService();

    /**
     * 首页
     */
    public void index(){
        forwardJsp("/pages/grade/index");

    }


    /**
     * 编辑页面
     */
    @Role({User.ROLE_ADMIN, User.ROLE_TEACHER})
    public void edit(Integer id){
        if (id!=null){
            Grade grade =gradeService.getById(id);
            getRequest().setAttribute("grade",grade);
        }
        forwardJsp("/pages/grade/edit");
    }

    public void save(String name,Integer id) throws ServletException, IOException {
        String result = gradeService.saveGrade(name,id);
        if(result==null){
            writeJSON(true);
        }else{
            writeJSON(false,result);
        }
    }
    public void list(String keywords) throws ServletException, IOException {
        List<Grade> list =gradeService.list(keywords);
        writeJSON(true,null,list);
    }


    public void del(Integer id) throws ServletException, IOException {
        String result = gradeService.del(id);
        if(result==null){
            writeJSON(true);
        }else{
            writeJSON(false,result);
        }
    }



}
